package com.qcby.shujia.demo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qcby.shujia.demo.common.web.PageData;
import com.qcby.shujia.demo.entity.Role;
import com.qcby.shujia.demo.entity.Tright;
import com.qcby.shujia.demo.entity.User;
import com.qcby.shujia.demo.entity.User_Role;
import com.qcby.shujia.demo.entity.vo.RoleVo;
import com.qcby.shujia.demo.entity.vo.UserVo;
import com.qcby.shujia.demo.mapper.UserMapper;
import com.qcby.shujia.demo.service.UserService;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @className: UserServiceImpl
 * @description: TODO 类描述
 * @author: whz
 * @date: 2021/9/19
 **/

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int deleteById(Integer id) {
        int i=userMapper.deleteById(id);
        userMapper.deleteur(id);

        return i;
    }

    @Override
    public List<User> selectAll() {
        List<User> li=userMapper.selectAll();
        return li;
    }

    @Override
    public int deletedisp(int[] ids) {
        int flag=1;
        int j;
        for (int i = 0; i <ids.length ; i++) {
            j=deleteById(ids[i]);
            if(j==0){
                flag=0;
            }
        }
        return flag;
    }

    @Override
    public UserVo selectUrById(Integer id) {
        UserVo userVo = userMapper.selectUrById(id);
        return userVo;
    }

    @Override
    public int insert(User user, List<Integer> roleids) {
        int i=userMapper.insert(user);
        int userid=user.getId();
        for (int j = 0; j <roleids.size() ; j++) {
            User_Role user_role = new User_Role();
            user_role.setUserid(userid);
            user_role.setRoleid(roleids.get(j));
            userMapper.insertur(user_role);
        }
        return i;
    }

    @Override
    public int update(User user, List<Integer> uproleids) {
        int i=userMapper.update(user);
        userMapper.deleteur(user.getId());
        for (int j = 0; j <uproleids.size() ; j++) {
            User_Role user_role = new User_Role();
            user_role.setUserid(user.getId());
            user_role.setRoleid(uproleids.get(j));
            userMapper.insertur(user_role);
        }
        return i;
    }
    public UserVo login(User user){
        UserVo uv=userMapper.login(user);
        if(uv!=null){//登陆成功
            return uv;
        }else {//登陆失败
            return null;
        }
    }

    @Override
    public PageData listpage(int pageNo, int pageSize) {
        Page<Object> page = PageHelper.startPage(pageNo, pageSize);
        userMapper.listpage();
        return PageData.build(page);
    }

    @Override
    public List<String> getAuthStrListByUser(UserVo user) {
        List<Integer> roleIdList=new ArrayList<>();
        for (int i = 0; i <user.getUrlist().size() ; i++) {
            roleIdList.add(user.getUrlist().get(i).getId());
        }
        List<String> authStrList= Arrays.asList("user:selectAll");
        return authStrList;
    }
}
